<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cart;

class CartController extends Controller
{
    //

    public function buy(Request $req){
    	// dd($req);

    	// 获取cookie中的信息,并将其转换成字符串
    	$productList = $req->headers;

    	foreach($productList as $k=>$v){
    		if($k=='cookie'){
    			// $url = substr(urldecode($v[0]),0,strpos(urldecode($v[0]),'XSRF-'));
    			$url = urldecode($v[0]);
		    	$pos1 = strpos($url,'"productList"');
		    	$pos2 = strpos($url,'}]}"');
		    	$url = substr($url,$pos1,($pos2-$pos1+2));
    		}
    	}
    	$url = '{'.$url.'}';
		//dd($url);

    	// 将字符串转换成json对象
    	$url = json_decode($url);

    	//获取订单的品名/单价/数量信息----------------------------
    	foreach($url as $v){$row = $v;}

    	$num = [];
    	$meals = [];
    	//$meals[]['num'] = 1;
    	for($i=0;$i<count($row);$i++){
    		$meals[] = DB::table('meals')->where('mid',$row[$i]->id[0])->first();
    		$num[] = [json_encode($row[$i]->num)];
			$meals = json_decode(json_encode($meals),true);
    	}
    	//print_r($meals);exit;

    	$m = DB::table('meals')->where('mid',$meals[0]['mid'])->first();
    	$stores = DB::table('stores')->where('sid',$m->sid)->first();

    	//计算订单的总金额totalValue --------------------------------

    	$totalValue = 0;
    	for($i=0;$i<count($meals);$i++){
    		$totalValue += ($meals[$i]['mprice'])*($num[$i][0]);
    	}
    	//dd($totalValue);

    	return view('index.shoppingcart',[
    		'stores' => $stores,
    		'meals' => $meals,
    		'num' => $num,
    		'totalValue' => $totalValue
    	]);
    }


    public function order(Request $req){
    	$productList = $req->headers;
    	foreach($productList as $k=>$v){
    		if($k=='cookie'){

    			$msg = urldecode($v[0]); 			// 获取总的信息

    			// 获取用户填写的地址 $u_adds --------------------
    			$pos1 = strpos($msg,'currentInputAddress');
    			$pos2 = strpos($msg,'currentInputTel');
    			$u_adds = substr($msg,$pos1,($pos2-$pos1-3));
    			$u_adds = str_replace('=','":',$u_adds);
    			$u_adds = '{"'.$u_adds.'"}';
    			//dd($u_adds);

    			// 获取用户填写的电话 $u_tels --------------------
    			$pos = strpos($msg,'currentInputTel');
    			$u_tels = substr($msg,($pos+16),13);
    			$u_tels = trim($u_tels,'"');
    			//dd($u_tels);

		    	// 获取订单信息 -------------------------------------
		    	$pos1 = strpos($msg,'"productList"');
		    	$pos2 = strpos($msg,'}]}"');
		    	$order = substr($msg,$pos1,($pos2-$pos1+2));
		    	$order = '{'.$order.'}';

    		}
    	}

    	$u_adds = json_decode($u_adds);
    	$u_tels = json_decode($u_tels);
    	$order = json_decode($order);

    	//获取订单的品名/单价/数量信息----------------------------
    	foreach($order as $v){$row = $v;}

    	$num = [];
    	$meals = [];
    	//$meals[]['num'] = 1;
    	for($i=0;$i<count($row);$i++){
    		$meals[] = DB::table('meals')->where('mid',$row[$i]->id[0])->first();
    		$num[] = [json_encode($row[$i]->num)];
			$meals = json_decode(json_encode($meals),true);
    	}

    	$mname = '|';
    	$mprice = '|';
    	$mnum = '|';
    	foreach($meals as $m){
    		$mname .= $m['mname'].'|';
    		$mprice .= $m['mprice'].'|';
    	}
    	foreach($num as $n){
    		$mnum .= $n[0].'|';
    	}
    	$ordtime = date('Y-m-d H:i:s',time());
    	//dd($ordtime);

    	//计算订单的总金额totalValue --------------------------------

    	$totalValue = 0;
    	for($i=0;$i<count($meals);$i++){
    		$totalValue += ($meals[$i]['mprice'])*($num[$i][0]);
    	}

    	// 生成订单号--------------------------
    	$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
		$ord_sn = $yCode[intval(date('Y')) - 2016] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
		//dd($ord_sn);

    	// 将订单信息写入数据库----------------------------------------------

    	$in_orders = DB::insert('insert into orders (ord_sn, mname, mprice, mnum, ordtime) values (?, ?, ? , ?, ? )',
        [$ord_sn, $mname, $mprice, $mnum, $ordtime] )? 1:0;

    	$in_ordinfos = DB::insert('insert into ordinfos (consignee, address, mobile, money, ordtime) values (?, ?, ?, ?, ? )',
        ['游客', $u_adds->currentInputAddress, $u_tels, $totalValue, $ordtime]) ? 1:0;

    	if($in_orders == 1 && $in_ordinfos == 1){
    		//return '订单提交成功!';
    		return redirect('/wycc');
    	}


    }
}
